package com.itboye.lottery.activity.user

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import com.itboye.lottery.R
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.Const
import com.itboye.lottery.utils.SPUtils
import kotlinx.android.synthetic.main.activity_edit_nickname.*

class EditNicknameActivity : BaseActivity(), ResponseCallback {
    override fun success(o: Any, requestSuccessType: String) {
        ByAlert.alert(o)
        SPUtils.put(this, null, Const.NICKNAME, nickname)
        finish()
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        ByAlert.alert(msg)
    }

    var tv_edit_nickname_sure: TextView? = null
    var userPresenter: UserPresenter? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_edit_nickname
    }

    override fun initData() {
        setTitle("修改昵称")
        et_nickname.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                setButtonEnable()
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        })
        userPresenter = UserPresenter(this)
    }

    private fun setButtonEnable() {
        if (et_nickname.text.isEmpty()) {
            tv_edit_nickname_sure!!.setBackgroundColor(resources.getColor(R.color.gray_898989))
            tv_edit_nickname_sure!!.isEnabled=false
        }else{
            tv_edit_nickname_sure!!.setBackgroundColor(resources.getColor(R.color.main_color))
            tv_edit_nickname_sure!!.isEnabled=true
        }
    }

    override fun onClick(p0: View?) {
        when (p0!!) {
            tv_edit_nickname_sure -> {
                updateNickName()
            }
            else -> {
            }
        }
    }

    private var nickname: String = ""

    private fun updateNickName() {
        nickname = et_nickname.text.toString()
        if (nickname.isEmpty()) {
            ByAlert.alert("请输入有效昵称")
            return
        }
        userPresenter!!.updateUserInfo(nickname)
    }
}